<style lang="less" scoped>
</style>
<template lang="html">
  <el-form :model="passForm" status-icon :rules="rules2" ref="passForm" label-width="100px" class="changePass">
  <el-form-item label="旧密码" prop="oldp">
    <el-input required v-model="passForm.oldp" placeholder="请输入旧密码" type="password" auto-complete="off"></el-input>
  </el-form-item>
  <el-form-item label="新密码" prop="newp">
    <el-input required v-model="passForm.newp" type="password" placeholder="请输入新密码" auto-complete="off"></el-input>
  </el-form-item>
  <el-form-item label="确认新密码" prop="confirmp">
    <el-input required v-model="passForm.confirmp" type="password" placeholder="请再次输入新密码" auto-complete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button @click="resetForm('passForm')">重置</el-button>
    <el-button type="primary" @click="submitForm('passForm')">提交</el-button>
  </el-form-item>
</el-form>
</template>

<script>
export default {
  data () {
    var confirmPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.passForm.newp) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var oldPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'))
      } else {
        if (this.passForm.oldpass !== '') {
          this.$refs.passForm.validateField('oldp')
        }
        callback()
      }
    }
    var newPass = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入新密码'))
      }
      setTimeout(() => {
        if (this.passForm.newp.length < 6 || this.passForm.newp.length > 16) {
          callback(new Error('密码必须在6~16个字符之间'))
        } else {
          callback()
        }
      }, 1000)
    }
    return {
      passForm: {
        oldp: '',
        newp: '',
        confirmp: ''
      },
      rules2: {
        oldp: [
          { validator: oldPass, trigger: 'blur' }
        ],
        newp: [
          { validator: newPass, trigger: 'blur' }
        ],
        confirmp: [
          { validator: confirmPass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
